define(function(require, exports, module) {

  var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('underscore');
  var jsonview = require('jsonview');
  require('css!../style.css')
  var tmpl = require('text!../tmpl.html')

  function main() {
    var $app = $('<div id="app">');
    $('#main').html($app);

    $app.append(tmpl)


    $('#screenshot').on('click', function() {
      let hash = hashParse(window.location.hash)
      hash.params.screenshot = Date.now()
      window.location.hash = hashStringify(hash)
    })

    $('#screenshot2').on('click', function() {
      chrome.runtime.sendMessage('npbpckeabaofnoomdmnioldmibonipni', 'screenshot')
    })
  }

  function hashParse(hash) {
    let hashSplitArr = hash.split('?');
    let hashObj = {
      path: hashSplitArr[0],
      params: {}
    }
    if (hashSplitArr[1]) {
      const hashParamsSplitArr = hashSplitArr[1].split('&');
      for (let i = 0; i < hashParamsSplitArr.length; i++) {
        if (hashParamsSplitArr[i]) {
          const hashKVSplitArr = hashParamsSplitArr[i].split('=')
          if (hashKVSplitArr[0]) {
            hashObj.params[hashKVSplitArr[0]] = hashKVSplitArr[1]
          }
        }
      }
    }

    return hashObj
  }

  function hashStringify(hashObj) {
    let hash = hashObj.path;
    let params = hashObj.params;
    let first = true;
    for (let param in params) {
      hash += (first ? '?' : '&') + param + '=' + params[param];
      first = false;
    }
    return hash
  }

  return main;
})
